/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * frmFactoryModel.java
 *
 * Created on 18-feb-2009, 18:35:39
 */
package gui.forms;

import gui.tools.Receiver;
import gui.tools.models.TMItemCategory;
import gui.tools.models.TMStation;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import lib.model.BufferedStation;
import lib.model.FactoryModel;
import lib.model.ItemCategory;
import lib.model.Station;

/**
 *
 * @author jorge
 */
public class FrmFactoryModel extends javax.swing.JDialog implements Receiver {

    private FactoryModel factory;
    private Receiver parent;
    private boolean add;
    private int index;

    /** Creates new form frmFactoryModel */
    public FrmFactoryModel(FactoryModel factory, Receiver parent) {
        initComponents();

        this.getRootPane().setDefaultButton(btnSave);

        tbCategories.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    btnCategoriesModifyActionPerformed(null);
                }
            }
        });

        tbStations.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    btnStationsModifyActionPerformed(null);
                }
            }
        });


        String fileString = "/res/icons/logo.png";
        try {
            Image icon = ImageIO.read(getClass().getResource(fileString));
            this.setIconImage(icon);
        } catch (java.io.IOException ioe) {
            System.out.println("Error loading file: " + fileString);
        }

        this.factory = factory;
        this.parent = parent;

        DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy-HH:mm");

        if (factory != null) {
            this.setTitle("Editing factory model :" + factory.getName());
            txtFMName.setText(factory.getName());
            txtWhenModified.setText(formatter.format(factory.getWhenModified().getTime()));

            Collection<ItemCategory> ics = factory.getItemCategories().values();
            ArrayList<ItemCategory> categories = new ArrayList<ItemCategory>();
            for (ItemCategory ic : ics) {
                categories.add(ic);
            }
            tbCategories.setModel(new TMItemCategory(categories));

            Collection<Station> stats = factory.getStations().values();
            ArrayList<Station> stations = new ArrayList<Station>();
            for (Station s : stats) {
                stations.add(s);
            }
            tbStations.setModel(new TMStation(stations));
        } else {
            this.setTitle("New factory model:");

            tbCategories.setModel(new TMItemCategory(new ArrayList<ItemCategory>()));
            tbStations.setModel(new TMStation(new ArrayList<Station>()));
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tpFactoryModel = new javax.swing.JTabbedPane();
        pnStations = new javax.swing.JPanel();
        pnSStations = new javax.swing.JPanel();
        lblStations = new javax.swing.JLabel();
        btnStationsDelete = new javax.swing.JButton();
        btnStationsModify = new javax.swing.JButton();
        btnStationsAdd = new javax.swing.JButton();
        scStations = new javax.swing.JScrollPane();
        tbStations = new javax.swing.JTable();
        pnItemCategories = new javax.swing.JPanel();
        pnICCategories = new javax.swing.JPanel();
        lblCategories = new javax.swing.JLabel();
        btnCategoriesDelete = new javax.swing.JButton();
        btnCategoriesModify = new javax.swing.JButton();
        btnCategoriesAdd = new javax.swing.JButton();
        scCategories = new javax.swing.JScrollPane();
        tbCategories = new javax.swing.JTable();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        lblFMName = new javax.swing.JLabel();
        txtFMName = new javax.swing.JTextField();
        txtWhenModified = new javax.swing.JTextField();
        lblWhenModified = new javax.swing.JLabel();

        setTitle("New Factory model");
        setMinimumSize(new java.awt.Dimension(578, 568));
        setModal(true);
        setResizable(false);

        lblStations.setText("Stations:");

        btnStationsDelete.setText("Delete");
        btnStationsDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStationsDeleteActionPerformed(evt);
            }
        });

        btnStationsModify.setText("Modify...");
        btnStationsModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStationsModifyActionPerformed(evt);
            }
        });

        btnStationsAdd.setText("Add...");
        btnStationsAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStationsAddActionPerformed(evt);
            }
        });

        tbStations.setAutoCreateRowSorter(true);
        tbStations.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scStations.setViewportView(tbStations);

        javax.swing.GroupLayout pnSStationsLayout = new javax.swing.GroupLayout(pnSStations);
        pnSStations.setLayout(pnSStationsLayout);
        pnSStationsLayout.setHorizontalGroup(
            pnSStationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnSStationsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnSStationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scStations, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
                    .addGroup(pnSStationsLayout.createSequentialGroup()
                        .addGroup(pnSStationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnSStationsLayout.createSequentialGroup()
                                .addComponent(lblStations)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 293, Short.MAX_VALUE))
                            .addGroup(pnSStationsLayout.createSequentialGroup()
                                .addComponent(btnStationsAdd)
                                .addGap(18, 18, 18)))
                        .addComponent(btnStationsModify)
                        .addGap(18, 18, 18)
                        .addComponent(btnStationsDelete))))
        );
        pnSStationsLayout.setVerticalGroup(
            pnSStationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnSStationsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblStations)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scStations, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(pnSStationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnStationsDelete)
                    .addComponent(btnStationsModify)
                    .addComponent(btnStationsAdd))
                .addContainerGap())
        );

        javax.swing.GroupLayout pnStationsLayout = new javax.swing.GroupLayout(pnStations);
        pnStations.setLayout(pnStationsLayout);
        pnStationsLayout.setHorizontalGroup(
            pnStationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnStationsLayout.createSequentialGroup()
                .addComponent(pnSStations, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnStationsLayout.setVerticalGroup(
            pnStationsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnSStations, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        tpFactoryModel.addTab("Stations", pnStations);

        lblCategories.setText("Item categories:");

        btnCategoriesDelete.setText("Delete");
        btnCategoriesDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCategoriesDeleteActionPerformed(evt);
            }
        });

        btnCategoriesModify.setText("Modify...");
        btnCategoriesModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCategoriesModifyActionPerformed(evt);
            }
        });

        btnCategoriesAdd.setText("Add...");
        btnCategoriesAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCategoriesAddActionPerformed(evt);
            }
        });

        tbCategories.setAutoCreateRowSorter(true);
        tbCategories.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scCategories.setViewportView(tbCategories);

        javax.swing.GroupLayout pnICCategoriesLayout = new javax.swing.GroupLayout(pnICCategories);
        pnICCategories.setLayout(pnICCategoriesLayout);
        pnICCategoriesLayout.setHorizontalGroup(
            pnICCategoriesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnICCategoriesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnICCategoriesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scCategories, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnICCategoriesLayout.createSequentialGroup()
                        .addComponent(btnCategoriesAdd)
                        .addGap(18, 18, 18)
                        .addComponent(btnCategoriesModify)
                        .addGap(18, 18, 18)
                        .addComponent(btnCategoriesDelete))
                    .addComponent(lblCategories))
                .addContainerGap())
        );
        pnICCategoriesLayout.setVerticalGroup(
            pnICCategoriesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnICCategoriesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCategories)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scCategories, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(pnICCategoriesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCategoriesDelete)
                    .addComponent(btnCategoriesModify)
                    .addComponent(btnCategoriesAdd))
                .addContainerGap())
        );

        javax.swing.GroupLayout pnItemCategoriesLayout = new javax.swing.GroupLayout(pnItemCategories);
        pnItemCategories.setLayout(pnItemCategoriesLayout);
        pnItemCategoriesLayout.setHorizontalGroup(
            pnItemCategoriesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnICCategories, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnItemCategoriesLayout.setVerticalGroup(
            pnItemCategoriesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnICCategories, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        tpFactoryModel.addTab("Item categories", pnItemCategories);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        lblFMName.setText("Name:");

        txtWhenModified.setEditable(false);

        lblWhenModified.setText("Last modification date:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tpFactoryModel, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblFMName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtFMName, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(lblWhenModified)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtWhenModified, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFMName)
                    .addComponent(txtFMName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblWhenModified)
                    .addComponent(txtWhenModified, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tpFactoryModel, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-588)/2, (screenSize.height-598)/2, 588, 598);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnStationsAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStationsAddActionPerformed
        add = true;
        if (setFactoryModel()) {
            new FrmStation(null, factory, this).setVisible(true);
        }
    }//GEN-LAST:event_btnStationsAddActionPerformed

    private void btnStationsModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStationsModifyActionPerformed
        index = tbStations.getSelectedRow();
        add = false;
        if (index >= 0) {
            TMStation tms = (TMStation) tbStations.getModel();
            Station station = tms.getStation(index);
            if (setFactoryModel()) {
                new FrmStation(station, factory, this).setVisible(true);
            }
        } else {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Please select a station.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_btnStationsModifyActionPerformed

    private void btnStationsDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStationsDeleteActionPerformed
        index = tbStations.getSelectedRow();
        if (index >= 0) {
            int delete = JOptionPane.showConfirmDialog(rootPane,
                    "Are you sure to delete the selected station?",
                    "Confirm your decision",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if (delete == JOptionPane.CANCEL_OPTION) {
                return;
            }
            TMStation tms = (TMStation) tbStations.getModel();
            tms.removeStation(index);
            tbStations.setModel(tms);
        } else {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Please select a station.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_btnStationsDeleteActionPerformed

    private void btnCategoriesAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCategoriesAddActionPerformed
        add = true;
        if (setFactoryModel()) {
            new FrmItemCategory(null, factory, this).setVisible(true);
        }
    }//GEN-LAST:event_btnCategoriesAddActionPerformed

    private void btnCategoriesModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCategoriesModifyActionPerformed
        index = tbCategories.getSelectedRow();
        add = false;
        if (index >= 0) {
            TMItemCategory tmic = (TMItemCategory) tbCategories.getModel();
            ItemCategory category = tmic.getItemCategory(index);
            if (setFactoryModel()) {
                new FrmItemCategory(category, factory, this).setVisible(true);
            }
        } else {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Please select an item category.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_btnCategoriesModifyActionPerformed

    private void btnCategoriesDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCategoriesDeleteActionPerformed
        index = tbCategories.getSelectedRow();
        if (index >= 0) {
            int delete = JOptionPane.showConfirmDialog(rootPane,
                    "Are you sure to delete the selected item category?",
                    "Confirm your decision",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if (delete == JOptionPane.CANCEL_OPTION) {
                return;
            }
            TMItemCategory tmic = (TMItemCategory) tbCategories.getModel();
            tmic.removeItemCategory(index);
            tbCategories.setModel(tmic);
        } else {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Please select an item category.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_btnCategoriesDeleteActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (setFactoryModel()) {
            parent.reciveObject(factory);
            this.setVisible(false);
        }
    }//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCategoriesAdd;
    private javax.swing.JButton btnCategoriesDelete;
    private javax.swing.JButton btnCategoriesModify;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnStationsAdd;
    private javax.swing.JButton btnStationsDelete;
    private javax.swing.JButton btnStationsModify;
    private javax.swing.JLabel lblCategories;
    private javax.swing.JLabel lblFMName;
    private javax.swing.JLabel lblStations;
    private javax.swing.JLabel lblWhenModified;
    private javax.swing.JPanel pnICCategories;
    private javax.swing.JPanel pnItemCategories;
    private javax.swing.JPanel pnSStations;
    private javax.swing.JPanel pnStations;
    private javax.swing.JScrollPane scCategories;
    private javax.swing.JScrollPane scStations;
    private javax.swing.JTable tbCategories;
    private javax.swing.JTable tbStations;
    private javax.swing.JTabbedPane tpFactoryModel;
    private javax.swing.JTextField txtFMName;
    private javax.swing.JTextField txtWhenModified;
    // End of variables declaration//GEN-END:variables

    public void reciveObject(Object object) {
        if (object instanceof Station || object instanceof BufferedStation) {
            Station station = (Station) object;
            TMStation tms = (TMStation) tbStations.getModel();
            if (add) {
                tms.addStation(station);
            } else {
                tms.setStation(index, station);
            }
            tbStations.setModel(tms);
            tbStations.tableChanged(new TableModelEvent(tbStations.getModel()));
        } else if (object instanceof ItemCategory) {
            ItemCategory category = (ItemCategory) object;
            TMItemCategory tmic = (TMItemCategory) tbCategories.getModel();
            if (add) {
                tmic.addItemCategory(category);
            } else {
                tmic.setItemCategory(index, category);
            }
            tbCategories.setModel(tmic);
            tbCategories.tableChanged(new TableModelEvent(tbCategories.getModel()));
        }
    }

    private boolean setFactoryModel() {
        String factoryName = txtFMName.getText();
        if (!factoryName.equals("")) {
            factory = new FactoryModel(factoryName);
            List<ItemCategory> ics =
                    ((TMItemCategory) tbCategories.getModel()).getItemCategories();
            for (ItemCategory ic : ics) {
                factory.addItemCategory(ic);
            }
            List<Station> stations =
                    ((TMStation) tbStations.getModel()).getStations();
            for (Station s : stations) {
                factory.addStation(s);
            }
            return true;
        } else {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Name cannot be empty.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
}
